home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / yak160src.lha / Yak_1.60_Src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-05  |  13.4 KB  |  501 lines

  1. /*
  2.  * Yak version 1.58
  3.  * ----------------
  4.  * [Yak == Yet Another K(?)ommodity
  5.  *
  6.  * There seems to be a profusion of commodities doing this or that.
  7.  * Heres mine, to do what I want it to:
  8.  *
  9.  *      AutoActivate windows (SunMouse)
  10.  *      ClickToFront, ClickToBack, ScreenCycle
  11.  *      Close/Zip/Shrink/Zoom/Turn a window via keyboard.
  12.  *      Bring up a palette on front screen.
  13.  *      Insert date into read-stream.
  14.  *      Produce key-click (like my keyclick program).
  15.  *      Some other things...
  16.  *
  17.  * Martin W. Scott & GaĆ«l Marziou, 8/93.
  18.  */
  19. #include <exec/types.h>
  20. #include <exec/libraries.h>
  21. #include <exec/memory.h>
  22. #include <devices/inputevent.h>
  23. #include <dos/dos.h>
  24. #include <dos/dostags.h>
  25. #include <graphics/displayinfo.h>
  26. #include <libraries/commodities.h>
  27. #include <libraries/reqtools.h>
  28. #include <libraries/locale.h>
  29. #include <intuition/intuitionbase.h>
  30. #include <clib/alib_protos.h>
  31. #include <clib/exec_protos.h>
  32. #include <clib/dos_protos.h>
  33. #include <clib/graphics_protos.h>
  34. #include <clib/commodities_protos.h>
  35. #include <clib/intuition_protos.h>
  36. #include <clib/locale_protos.h>
  37. #include <clib/reqtools_protos.h>
  38.  
  39. #ifdef __SASC
  40. #  include <pragmas/exec_pragmas.h>
  41. #  include <pragmas/dos_pragmas.h>
  42. #  include <pragmas/graphics_pragmas.h>
  43. #  include <pragmas/commodities_pragmas.h>
  44. #  include <pragmas/intuition_pragmas.h>
  45. #  include <pragmas/locale_pragmas.h>
  46. #  include <pragmas/reqtools.h>
  47. #else
  48. #  include <proto/exec.h>
  49. #  include <proto/dos.h>
  50. #  include <proto/graphics.h>
  51. #  include <proto/commodities.h>
  52. #  include <proto/intuition.h>
  53. #  include <proto/locale.h>
  54. #  include <proto/reqtools.h>
  55. extern struct Library *DOSBase;
  56. #endif
  57.  
  58.  
  59. #include <string.h>
  60. #include <stdarg.h>
  61.  
  62. #include "code.h"
  63. #include "yak.h"
  64. #include "hotkey_types.h"
  65. #include "beep.h"
  66. #include "icon.h"
  67. #include "version.h"
  68.  
  69. #define CATCOMP_BLOCK
  70. #define CATCOMP_NUMBERS
  71. #include "locale/yak_locale_strings.h"
  72. #undef CATCOMP_BLOCK
  73.  
  74. #include "WB2CLI.h"     /* we'll be a shell process */
  75. #define DEF_CURRENTDIR  "SYS:"
  76.  
  77.  
  78.  
  79. /* local prototypes for main.c */
  80. static void CloseResources(void);
  81. static BOOL OpenResources(void);
  82. static void FreePatterns(void);
  83. static LONG ProcessMsg(void);
  84. void MAIN(void);
  85.  
  86. LONG (*HandleIDCMP)(void);
  87.  
  88. extern struct WBStartup *WBMsg;
  89. /*
  90.  *  libraries opened by startup code; basepointers needed by function pragmas
  91.  */
  92. extern struct Library *SysBase, *DOSBase;
  93.  
  94. /* global data - library bases and the like */
  95. struct Library  *CxBase, *IconBase,
  96.     *GadToolsBase, *LayersBase,
  97.     *WorkbenchBase, *LocaleBase,
  98.     *GfxBase, *IntuitionBase,
  99.     *KeymapBase;
  100. struct Locale *locale;
  101. struct Catalog *Catalog;
  102. struct MsgPort *broker_mp;
  103. CxObj *broker;
  104. char *PopKeyStr;
  105. #define POPKEY_EVENT    1L      /* cannot clash with YHK event... */
  106.  
  107. static const char *versionstr=VERSION_STR;
  108.  
  109. struct NewBroker newbroker = {
  110.     NB_VERSION,
  111.     "Yak",                                              /* string to identify this broker */
  112.     VERSION_BROKER,
  113.     "Multi-purpose commodity",
  114.     NBU_UNIQUE | NBU_NOTIFY,    /* Don't want any new commodities
  115.                                  * starting with this name.  If someone
  116.                                  * tries it, let me know */
  117.     COF_SHOW_HIDE
  118.     };
  119.  
  120. ULONG           wndsigflag;             /* here for overlay purposes */
  121. ULONG           cxsigflag;
  122. ULONG           invariantsigflag;
  123. BYTE            palette_count;          /* how many palettes are open */
  124.  
  125.  
  126. /* from handler.c */
  127. extern ULONG    clicksigflag, intuiopsigflag, blankscreensigflag;
  128. extern void (*intui_routine)(APTR);     /* for intui_op's */
  129. extern APTR intui_parameter;
  130.   
  131. /* from icon.c */
  132. extern ULONG    appsigflag;
  133.  
  134. /* close what we opened */
  135. static void
  136.     CloseResources()
  137. {
  138.     /* NULL pointers are valide so don't waste time to test them */
  139.     CloseLibrary(IntuitionBase);
  140.     CloseLibrary(GfxBase);
  141.     CloseLibrary(CxBase);
  142.     CloseLibrary(LayersBase);
  143.     CloseLibrary(IconBase);
  144.     CloseLibrary(GadToolsBase);
  145.     CloseLibrary(WorkbenchBase);
  146.     CloseLibrary(KeymapBase);
  147.     if (LocaleBase)
  148.     {
  149.         CloseCatalog (Catalog); 
  150.         CloseLocale(locale);
  151.         CloseLibrary(LocaleBase);
  152.     }
  153. }
  154.  
  155. /* open libraries, devices that we need */
  156. static BOOL
  157.     OpenResources(void)
  158. {
  159.     if ((IntuitionBase = OpenLibrary("intuition.library", 37L)) &&
  160.         (GfxBase       = OpenLibrary("graphics.library", 37L)) &&
  161.         (CxBase        = OpenLibrary("commodities.library", 37L)) &&
  162.         (LayersBase    = OpenLibrary("layers.library", 37L)) &&
  163.         (IconBase      = OpenLibrary("icon.library", 37L)) &&
  164.         (GadToolsBase  = OpenLibrary("gadtools.library", 37L)) &&
  165.         (WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
  166.         (KeymapBase    = OpenLibrary("keymap.library", 37L)))
  167.     {
  168.         return TRUE;
  169.     }
  170.     CloseResources();
  171.     return FALSE;
  172. }
  173.  
  174.  
  175. /* open locale library and catalog */
  176. void
  177.     OpenLocaleStuff(char *language)
  178. {
  179.     
  180.     if (LocaleBase =(struct LocaleBase *)OpenLibrary("locale.library", 38L))
  181.     {
  182.         if (!(locale = OpenLocale(NULL)))
  183.         {       
  184.             PostError("No locale set!");
  185.             CloseLibrary(LocaleBase);
  186.             LocaleBase = NULL;
  187.         }
  188.         Catalog = OpenCatalog(  locale, "yak.catalog", 
  189.                               OC_BuiltInLanguage, "english", 
  190.                               OC_Language, language,        
  191.                               OC_Version, 15L, 
  192.                               TAG_DONE );
  193.     }       
  194. }
  195.  
  196.  
  197. /* slighlty modified version of GetString() generated by catcomp */
  198.  
  199. char 
  200.     *getString(ULONG MsgID)
  201. {
  202.     LONG   *l;
  203.     UWORD  *w;
  204.     char *builtIn;
  205.     
  206.     l = (LONG *)CatCompBlock;
  207.     
  208.     while (*l != MsgID )
  209.     {
  210.         w = (UWORD *)((ULONG)l + 4);
  211.         l = (LONG *)((ULONG)l + (ULONG)*w + 6);
  212.     }
  213.     builtIn = (char *)((ULONG)l + 6);
  214.     if (LocaleBase)
  215.         return (GetCatalogStr (Catalog, MsgID, builtIn));
  216.     return(builtIn);
  217. }
  218.  
  219. /* simple requester with args */
  220. void
  221.     PostError(char *body, ... )
  222. {
  223.     struct EasyStruct es;
  224.     va_list args;
  225.     
  226.     if (!IntuitionBase)
  227.     {
  228.         Write(Output(), "Need AmigaDos 2.0+\n", -1);
  229.         return;
  230.     }
  231.     
  232.     /* setup the argument array */
  233.     va_start( args, body );
  234.     
  235.     /* initialise the structure */
  236.     es.es_StructSize = sizeof(struct EasyStruct);
  237.     es.es_Flags = 0L;
  238.     es.es_Title = getString(Error_Requester_Title);
  239.     es.es_TextFormat = body;
  240.     es.es_GadgetFormat = "OK";
  241.     
  242.     /* display the requester */
  243.     EasyRequestArgs(NULL, &es, NULL, args);
  244.     
  245.     /* free the arguments */
  246.     va_end( args );
  247. }
  248.  
  249. /* parse pattern, report errors */
  250. __regargs BOOL
  251.     InitPattern(char *newpatstr, UWORD n)
  252. {
  253.     char *patstr = newpatstr ? newpatstr : patterns[n].patstr;
  254.     char *pat;
  255.     LONG len;
  256.     
  257.     if (pat = AllocVec(len = strlen(patstr)*3+10, MEMF_CLEAR))
  258.     {
  259.         if (ParsePattern(patstr, pat, len) != -1)
  260.         {
  261.             if (newpatstr) strncpy(patterns[n].patstr, newpatstr, PATLEN);
  262.             if (patterns[n].pat) FreeVec(patterns[n].pat);
  263.             patterns[n].pat = pat;
  264.             return TRUE;
  265.         }
  266.         
  267.         PostError("%s:\n\"%s\"", getString(Parsing_Pattern_ERR), patstr);
  268.         FreeVec(pat);
  269.     }
  270.     else PostError(getString(Allocation_ERR));
  271.     return FALSE;
  272. }
  273.  
  274. static void
  275.     FreePatterns()
  276. {
  277.     UWORD i;
  278.     
  279.     for (i = 0; i < NUM_PATTERNS; i++)
  280.         if (patterns[i].pat) FreeVec(patterns[i].pat);
  281. }
  282.  
  283.  
  284. #ifdef _DCC
  285. void _waitwbmsg(void);
  286. static void
  287.     uncalled(void)
  288. {
  289.     _waitwbmsg();
  290. }
  291. #endif
  292.  
  293.  
  294.  
  295. void 
  296.     MAIN()             /* Yak: multi-function commodity */
  297. {
  298.     BPTR    newdir = NULL, olddir;
  299.     
  300.     if (OpenResources())
  301.     {
  302.         if (broker_mp = CreateMsgPort())
  303.         {
  304.             newbroker.nb_Port = broker_mp;
  305.             cxsigflag = 1L << broker_mp->mp_SigBit;
  306.             
  307.             /* process tool-types */
  308.             GetOurIcon(WBMsg);
  309.             newbroker.nb_Pri = (BYTE)TTInt("CX_PRIORITY", 0);
  310.             
  311.             if (WBMsg)
  312.             {
  313.                 if (newdir = Lock(DEF_CURRENTDIR, ACCESS_READ))
  314.                     olddir = CurrentDir(newdir);
  315.                 WB2CLI(WBMsg,4000,(struct DosLibrary *)DOSBase); /* get it over with... */
  316.             }
  317.             
  318.             if (broker = CxBroker(&newbroker, NULL))
  319.             {
  320.                 /* HANDLER FIRST, SO IT SEES EVERYTHING!!! */
  321.                 if (InitHandler())
  322.                 {       
  323.                     InitYakHotKeyList();
  324.                     LoadSettings();
  325.                     
  326.                     /* Open the right locale if tooltype LANGUAGE is used */
  327.                     OpenLocaleStuff(TTString("LANGUAGE",NULL));
  328.                     
  329.                     if (PopKeyStr = DupStr(TTString("CX_POPKEY", "Rcommand help")))
  330.                     {
  331.                         CxObj *tmpobj;
  332.                         
  333.                         if (tmpobj = HotKey(PopKeyStr, broker_mp, POPKEY_EVENT))
  334.                             AttachCxObj(broker, tmpobj);
  335.                         else
  336.                             PostError("%s:\"%s\"", getString(CX_POPKEY_invalid_ERR), 
  337.                                       PopKeyStr);
  338.                     }
  339.                     /* else... if this failed, we lose */
  340.                     
  341.                     MyPri(ACTIVE);
  342.                     ActivateCxObj(broker, 1L);
  343.                     
  344.                     if (TTBool("CX_POPUP", FALSE))
  345.                         ShowYakInterface();
  346.                     
  347.                     if (TTBool("APPICON", FALSE))
  348.                     {
  349.                         if (!MakeOurAppIcon(TTString("ICONNAME", "Yak!")))
  350.                             if (WBMsg)
  351.                                 PostError(getString(Couldn_t_create_AppIcon_ERR));
  352.                     }
  353.                     /* WB makes a copy of icon, so can free it */
  354.                     FreeOurIcon();
  355.                     
  356.                     /* these are the signals waited for, + window sig */
  357.                     invariantsigflag = SIGBREAKF_CTRL_C | cxsigflag
  358.                         | clicksigflag | intuiopsigflag 
  359.                             | appsigflag | blankscreensigflag;
  360.                     
  361.                     while (ProcessMsg())
  362.                         ;
  363.                     HideInterface();
  364.                     RemoveOurAppIcon();
  365.                     DeleteYakHotKeyList();
  366.                     
  367.                     FreeStr(PopKeyStr);
  368.                     MyPri(ORIGINAL);
  369.                     
  370.                     EndHandler();
  371.                     FreePatterns();
  372.                 }
  373.                 else 
  374.                     PostError(getString(Allocation_ERR));
  375.                 
  376.                 DeleteCxObjAll(broker);
  377.             }
  378.             
  379.             if (newdir) 
  380.             {
  381.                 CurrentDir(olddir);
  382.                 UnLock(newdir);
  383.             }
  384.             DeleteMsgPort(broker_mp);
  385.             FreeOurIcon();      /* may already be gone, but so what? */
  386.         }
  387.         else PostError(getString(Allocation_ERR));
  388.         
  389.         CloseResources();
  390.         MWReport("At end of main()", MWR_FULL); /* Generate a memory usage report */
  391.     }
  392.     else PostError(getString(Resource_ERR));
  393. }
  394.  
  395.  
  396.  
  397.  
  398. /* monitor cx port, act on messages */
  399. static LONG
  400.     ProcessMsg(void)
  401. {
  402.     CxMsg *msg;
  403.     ULONG sigrcvd, msgid, msgtype;
  404.     LONG returnvalue = 1L;
  405.     
  406.     sigrcvd = Wait(invariantsigflag | wndsigflag);
  407.     
  408.     if (sigrcvd & intuiopsigflag) 
  409.     {       
  410.         /* intuiop requested */
  411.         intui_routine(intui_parameter);
  412.     }
  413.     
  414.     
  415.     if ((sigrcvd & clicksigflag) && click_volume) /* keyclick please */
  416.     {
  417.         beep(click_volume);
  418.         Delay(1);               /* avoid ugly sound when key repeating */
  419.     }
  420.     
  421.     if (sigrcvd & blankscreensigflag) /* blank screen please */
  422.     {
  423.         BlankScreen();
  424.     }
  425.     
  426.     if (sigrcvd & appsigflag)   /* settings change */
  427.     {
  428.         RespondToAppIcon();
  429.         ShowYakInterface();
  430.     }
  431.     
  432.     if (sigrcvd & wndsigflag)   /* settings change */
  433.         if ((*HandleIDCMP)() != ROOT_OKAY)
  434.             returnvalue = 0;
  435.         else 
  436.             if (!wndsigflag)    /* window gone */
  437.                 DummyOverlay(); 
  438.     /* remove code */
  439.     
  440.     while(msg = (CxMsg *)GetMsg(broker_mp))
  441.     {
  442.         msgid = CxMsgID(msg);
  443.         msgtype = CxMsgType(msg);
  444.         ReplyMsg((struct Message *)msg);
  445.         
  446.         switch(msgtype)
  447.         {
  448.           case CXM_IEVENT:
  449.             
  450.             if (msgid == POPKEY_EVENT)
  451.                 ShowYakInterface();
  452.             else
  453.                 /* a generic hotkey... */
  454.                 PerformAction((YakHotKey *)msgid);
  455.             break;
  456.             
  457.           case CXM_COMMAND:
  458.             switch(msgid)
  459.             {
  460.               case CXCMD_UNIQUE:
  461.               case CXCMD_APPEAR:
  462.                 ShowYakInterface(); /* check error return? */
  463.                 break;
  464.                 
  465.               case CXCMD_DISAPPEAR:
  466.                 HideInterface();
  467.                 DummyOverlay();
  468.                 break;
  469.                 
  470.               case CXCMD_DISABLE:
  471.                 ActivateCxObj(broker, 0L);
  472.                 TurnMouseOn();
  473.                 break;
  474.                 
  475.               case CXCMD_ENABLE:
  476.                 ActivateCxObj(broker, 1L);
  477.                 break;
  478.                 
  479.               case CXCMD_KILL:
  480.                 returnvalue = 0L;
  481.                 break;
  482.             }
  483.             break;
  484.         }
  485.     }
  486.     
  487.     if (sigrcvd & SIGBREAKF_CTRL_C)
  488.         returnvalue = 0L;
  489.     
  490.     if (!returnvalue && !OkayToExit())
  491.     {
  492.         PostError(getString(Cannot_exit_palette_opened_ERR));
  493.         returnvalue = 1;
  494.     }
  495.     
  496.     return(returnvalue);
  497. }
  498.  
  499.  
  500.  
  501.